<template>
  <a-modal   :width='400'
  title="退税录入"
  :visible="visible"
  @ok="handleOk"
  :confirmLoading="confirmLoading"
  @cancel="handleCancel"
  >
    <a-form   :form="form"  > 
             <a-form-item
              label="日期"
              :label-col='{ span : 6}'  :wrapper-col="{ span : 18 }" 
              >
              <a-date-picker    v-decorator="['taxRefundTime',formRule.taxRefundTime]" />
            </a-form-item>   
            <a-form-item
              label="退税金额"
             :label-col='{ span : 6}'  :wrapper-col="{ span : 18 }" 
              >
                <!-- <a-input-group compact  v-decorator='["taxRefundAmt",formRule.taxRefundAmt]'>   v-decorator='["taxRefundAmtUnit",formRule.taxRefundAmtUnit]' -->
                    <a-select   style="width: 30%"  v-model='taxRefundAmtUnit'  @change='(val)=>change(val)'>  
                      <a-select-option value="1">USD</a-select-option>
                      <a-select-option value="2">GBP</a-select-option>
                      <a-select-option value="3">EUR</a-select-option>
                      <a-select-option value="4">HKD</a-select-option>
                      <a-select-option value="5">CNY</a-select-option>
                    </a-select>
                    <a-input style="width: 70%"  type='number'  v-decorator='["taxRefundAmt",formRule.taxRefundAmt]'/>
                <!-- </a-input-group> -->

            </a-form-item> 
            <a-form-item  style='display: none;'> 
                <a-input  v-decorator='["taxRefundAmtUnit",formRule.taxRefundAmtUnit]' />
            </a-form-item>
            <a-form-item
              label="退税税率"
              :label-col='{ span : 6}'  :wrapper-col="{ span : 18 }" 
              >
              <a-input placeholder=""   v-decorator="['taxRefundRate',formRule.taxRefundRate]"/> 
            </a-form-item>
            <a-form-item
              label="退税状态"
               :label-col='{ span : 6}'  :wrapper-col="{ span : 18 }" 
              >
              <a-select       v-decorator="['taxRefundStatus',formRule.taxRefundStatus]">  
                    <a-select-option value="1">未申请</a-select-option>
                    <a-select-option value="2">已申请，等待中</a-select-option>
                    <a-select-option value="3">已完成</a-select-option> 
                  </a-select>
            </a-form-item>
             <a-form-item
              label="退税凭证"
               :label-col='{ span : 6}'  :wrapper-col="{ span : 18 }" 
              >
               <fileUpload    multiple v-on:uploading='uploading($event)'  v-on:removeFile='remove($event)'  :fileList='fileData'  ></fileUpload>
                <a-input  v-decorator='["fileList",formRule.fileList]'  style='display: none;'/>
            </a-form-item>  

             <a-form-item  style='display: none;'>
              <a-input   v-decorator="['orderCode',formRule.orderCode]"/>
            </a-form-item> 
            <a-form-item  style='display: none;'>
              <a-input   v-decorator="['prjCode',formRule.prjCode]"/>
            </a-form-item> 
            
            <a-form-item  style='display: none;'>
              <a-input   v-decorator="['code',formRule.code]"/>
            </a-form-item> 
          </a-form>
     
  </a-modal>
</template>

<script>
  import  { submitRefund }  from  '@/api/order/index'
  import moment  from  'moment'

    import  fileUpload  from  '@/components/common/fileUpload'
export default {
  name: 'approateRec',
  props:{
      visible:{
        type:Boolean,
        default:false
      },
      // entCode:{
      //   type:String,
      //   default:''
      // }
  },
  data () {
    return { 
      isEdit:false,
      confirmLoading: false,
      taxRefundAmtUnit:'1',//绑定的单位
      formRule:{
        orderCode:{initialValue:''},
        prjCode:{initialValue:''},
        code:{initialValue:''},
 
        taxRefundTime:{ rules : [ { required:true,message:'请选择日期' } ],initialValue:null },
        taxRefundAmt:{ rules : [ { required:true,message:'请输入' },{ whitespace : true }, ],initialValue:null }, 
        taxRefundAmtUnit:{ initialValue:'1' },
        taxRefundRate:{ rules : [ { required:true,message:'请输入' },{ whitespace : true } ],initialValue:'' },
        taxRefundStatus:{ rules : [ { required:true,message:'请输入' },{ whitespace : true } ],initialValue:'' },
        // billNum:{ rules : [ { required:true,message:'请选择日期' } ] },
        // billType:{ rules : [ { required:true,message:'请输入' }] },
        // telexReleaseFlag:{ rules : [ { required:true,message:'请选择' } ] },
      },
      fileData:[],  
 
    }
  },
  methods:{
    //提交 信息
    handleOk(e) {
      let that = this , url 
        this.form.validateFields((err, values) => {
          console.log(values)
          if (!err) {  
            // this.confirmLoading = true;   
             /*上传文件处理*/
             if(this.fileData.length>0){
                values.fileList = this.fileData.map((item)=>{
                    return {
                        code:item.status == 'done' ? (item.code || item.response.data.code) :''
                      }
                }) 
             }
            if(this.isEdit == false){
              url='/op/tax_refund/create_tax_refund'
            }
            if(this.isEdit == true){
              url='/op/tax_refund/modify_tax_refund'//修改 退税
            }
            submitRefund(url,values).then((res)=>{
                if(res.data.flag ==true){
                  this.$message.success(res.data.msg)
                  this.$emit('handleOk')
                }
             })
          }
          else{
            console.log(err)
          }
        });
    },
    handleCancel(e) {
      this.$emit('handleCancel') 
    },
 
    change(val){
       this.form.setFieldsValue({
          taxRefundAmtUnit:val
       })
    },
    /*功能函数*/
    /*上传文件组件 上传完毕*/ 
    uploading(e){
      // console.log(e)
      this.fileData = e.fileList
    }, 
     /*移除文件*/
    remove(e){
      // console.log(e)
      this.fileData.map((item,index)=>{
        if(e.code == item.code){
            this.fileData.splice(index,1)
        }
      })
    },
 
  },
  mounted(){
    this.$nextTick(()=>{
      this.$on('openDialog',function(orderCode,prjCode){
        this.isEdit = false
        /*清空表单输入和校验*/
        this.form.resetFields()

        this.formRule.orderCode.initialValue = orderCode
        this.formRule.prjCode.initialValue = prjCode
        

      })
      this.$on('editDialog',function(data){
        console.log(data)
        this.isEdit = true
        /*清空表单输入和校验*/
        this.form.resetFields() 
        this.formRule.code.initialValue = data.code
        this.formRule.orderCode.initialValue = data.orderCode
        this.formRule.prjCode.initialValue = data.prjCode

        this.formRule.taxRefundTime.initialValue = data.taxRefundTime ? moment(data.taxRefundTime) : null
        this.formRule.taxRefundAmt.initialValue = String(data.taxRefundAmt)
        // this.formRule.test.initialValue = data.taxRefundAmt
        this.formRule.taxRefundAmtUnit.initialValue = data.taxRefundAmtUnit
        this.taxRefundAmtUnit = data.taxRefundAmtUnit //v-model绑定的值

        this.formRule.taxRefundRate.initialValue = data.taxRefundRate
        this.formRule.taxRefundStatus.initialValue = data.taxRefundStatus

        if(data.fileList && data.fileList.length>0){
            this.fileData = data.fileList.map((item)=>{
              return {
                uid:item.code,//uid ：default 默认类型回填 ；删除 
                code:item.code,//提交表单时 需要取跟上传文件处理后返回同样的code   上传文件后取code
                name: item.name,
                status: 'done',  
              }
            })
        }
        

      })
    })
  },
  computed: {
 
  },
  beforeCreate () {
    this.form = this.$form.createForm(this);
    // this.form.getFieldDecorator('keys', { initialValue: [0], preserve: true });
  },
  components:{
    fileUpload
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>
